package com.foodlocker.maintenance;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.foodlocker.common.CommonFunction;
import com.foodlocker.common.Constant;
import com.foodlocker.machine.Box;
import com.foodlocker.machine.R;



import android.content.Context;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.JsonReader;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class BoxManageFragment extends Fragment{
    
	
	List<Map<String, Object>> box_list;
	MyListAdapter myListAdapter;
	ListView listView;
	TextView hint;
	Button btn_open_all;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
	 //return super.onCreateView(inflater, container, savedInstanceState);
		 View view =inflater.inflate(R.layout.fragment_boxmanage, container, false);
		listView= (ListView) view.findViewById(R.id.listview_boxmanage);
		box_list=new ArrayList<Map<String,Object>>();
	
		
		myListAdapter=new MyListAdapter(this.getActivity(), box_list);			
		listView.setAdapter(myListAdapter);
		hint= (TextView) view.findViewById(R.id.tv_hint_boxmanage);
		btn_open_all= (Button) view.findViewById(R.id.btn_openall_boxmanage);
		
		myListAdapter.notifyDataSetChanged();
		return view;
	}

	
	 private class MyListAdapter extends BaseAdapter {
		   
		 public List<Map<String, Object>> data;  
		    private LayoutInflater layoutInflater;  
		    private Context context;  
		    
		public MyListAdapter(Context context,List<Map<String, Object>> data) {
				super();
				this.data = data;		
				this.context = context;
			    this.layoutInflater=LayoutInflater.from(context); 
			}
		
		
		final class Zujian_BoxManage{  
		    public TextView tv_boxstate,tv_boxid;  
	      
	       public Button btn_open,btn_lock,btn_unlock;  
	     
	     
	    }   //end of Zujian

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			 return box_list.size();  
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			 return box_list.get(position);  
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position; 
		}
	  
		@Override
		public View getView(int position, View  convertView, ViewGroup parent) {
			final int selectID = position;   
			 Zujian_BoxManage zujian=null;  
		        if(convertView==null){  
		            zujian=new Zujian_BoxManage();  
		            //获得组件，实例化组件  
		            convertView=layoutInflater.inflate(R.layout.listlayout_boxmanage, null);  
		          
		            zujian.tv_boxid=(TextView) convertView.findViewById(R.id.tv_boxid_boxmanage_list);
		            zujian.tv_boxstate=(TextView) convertView.findViewById(R.id.tv_boxstate_boxmanage_list); 
		           
		            zujian.btn_open=(Button) convertView.findViewById(R.id.btn_open_boxmanage_list);
		            zujian.btn_lock=(Button) convertView.findViewById(R.id.btn_lock_boxmanage_list);
		            zujian.btn_unlock=(Button) convertView.findViewById(R.id.btn_unlock_boxmanage_list);
		            convertView.setTag(zujian);  
		            System.out.println("position----"+position+"------inflated");
		        }else{   System.out.println("position----"+position+"-------not inflated");
		            zujian=(Zujian_BoxManage)convertView.getTag();  
		        } 
		        
		        String temp=(String) box_list.get(position).get("boxid");
		        zujian.tv_boxid.setText(temp); 
		        temp=(String) box_list.get(position).get("boxstate");
		        int boxstate= Integer.parseInt(temp);
		          switch(boxstate){
		          case 1: { zujian.tv_boxstate.setText("正常 空闲");  zujian.btn_unlock.setEnabled(false);  zujian.btn_lock.setEnabled(true);break;  }
		          case 2: { zujian.tv_boxstate.setText("故障 锁定");   zujian.btn_lock.setEnabled(false); zujian.btn_unlock.setEnabled(true);  break;  }
		          case 3: { zujian.tv_boxstate.setText("正常 使用中");  zujian.btn_unlock.setEnabled(false); zujian.btn_lock.setEnabled(true); break; }
		          
		          }
		          zujian.btn_open.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							String boxid= (String) box_list.get(selectID).get("boxid");
							 Message msg = new Message();              
			                 msg.what=1;
			                 msg.obj=boxid;
			                 Box.childHander.sendMessage(msg);
			                 Log.i("DepositActivity", " Send a message to the child thread -msg.what= "
			                 +msg.what+ " smg.obj=" + msg.obj);	
										
				                 } 
					});
		          
		          zujian.btn_lock.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
					
						
				                 } 
					});
		          
		          zujian.btn_unlock.setOnClickListener(new View.OnClickListener() {
						
						@Override
						public void onClick(View v) {
							
				                 } 
					});
		        return convertView;
		}// end of getview()
	 }
		 class HttpPostTaskforLock extends android.os.AsyncTask<String,String,String>{				
				int selectID;
                

					public HttpPostTaskforLock(int selectID) {
					super();
					this.selectID = selectID;
				}


					@Override
					protected String doInBackground(String... params) {
						// TODO Auto-generated method stub
						  String result=CommonFunction.UniversalHttpPost(params);			             
						return result;
					}
					
					

			@Override
				protected void onPostExecute(String result) {
					// TODO Auto-generated method stub
					super.onPostExecute(result);
					
				       
					}  
			 } //end of lock HttpPost	
		
	
	 private void define_all_listener(){
		 btn_open_all.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					for(int i=1;i<=Constant.NUMBER_BOXES_OF_MACHINE;i++){
					 Message msg = new Message();              
	                 msg.what=1;
	                 msg.obj=String.valueOf(i);
	                 Box.childHander.sendMessage(msg);
	                 Log.i("DepositActivity", " Send a message to the child thread -msg.what= "
	                 +msg.what+ " smg.obj=" + msg.obj);	}
								
		                 } 
			} );
	 }
	 
}
